package gmomo.test.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

import org.jivesoftware.smack.packet.Message;

public class MessageFrame extends JFrame implements ActionListener {
	
	private JTextArea inputArea;
	private JTextPane displayPane;
	private JButton btnInvite;
	private JButton btnSend;
	
	private MainFrame main;
	private Contact contact;
	
	public MessageFrame(MainFrame main, Contact contact) {
		super(contact.toString());
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		this.main = main;
		this.contact = contact;
		
		setupGUI();
		showFrame();
	}
	
	public void showFrame() {
		setLocation(main.getX(), main.getY());
		pack();
		setVisible(true);
	}
	
	private void setupGUI() {
		Container contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());
		
		displayPane = new JTextPane();
		displayPane.setEditable(false);
		Dimension displayDim = new Dimension(300, 200);
		displayPane.setPreferredSize(displayDim);
		
		inputArea = new JTextArea();
		inputArea.setLineWrap(true);
		inputArea.setWrapStyleWord(true);
		Dimension inputDim = new Dimension(300, 100);
		inputArea.setPreferredSize(inputDim);

		inputArea.addKeyListener(
				new KeyListener() {

					public void keyPressed(KeyEvent e) {
					}

					public void keyReleased(KeyEvent e) {
					}

					public void keyTyped(KeyEvent e) {
						if ( e.getKeyChar() == '\n' && e.isShiftDown() ) {
							inputArea.append("\n");
						}else if ( e.getKeyChar() == '\n' ) {
							String text = inputArea.getText();
							text = text.substring(0, text.length()-1);
							if ( text.length() > 0 )
								appendOwnText( text );
						}
					}
				});
		
		btnInvite = new JButton("invite");
		btnInvite.addActionListener(this);
		btnSend = new JButton("send");
		btnSend.addActionListener(this);
		
		JScrollPane input = new JScrollPane(inputArea);
		input.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		input.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		JScrollPane display = new JScrollPane(displayPane);
		JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT, display, input);
		
		contentPane.add(split, BorderLayout.CENTER);
		
		JPanel southPanel = new JPanel(new GridLayout(1, 2));
		southPanel.add(btnInvite);
		southPanel.add(btnSend);
		
		contentPane.add(southPanel, BorderLayout.SOUTH);
	}
	
	public void actionPerformed(ActionEvent e) {
		if ( e.getSource() == btnSend ) {
			appendOwnText( inputArea.getText() );
		}else if ( e.getSource() == btnInvite ) {
			new InvitationDialog(main, contact.getBareAddress());
		}
	}
	
	private void sendMsg(String text) {
		Message msg = new Message(contact.getXmppAddress());
		msg.setBody(text);
		main.send(msg);
	}
	
	public void update(String text) {
		appendOtherText(text);
	}
	
	private void appendText(String text, Color c) {
		SimpleAttributeSet aset = new SimpleAttributeSet();
	    StyleConstants.setForeground(aset, c);
	    Document doc = displayPane.getDocument();
	    try {
			doc.insertString(doc.getLength(), text + "\n", aset);
			displayPane.setCaretPosition( doc.getLength() );
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
	}
	
	private void appendOwnText(String text) {
		appendText(text, Color.BLACK);
		inputArea.setText("");
		sendMsg(text);
	}
	
	private void appendOtherText(String text) {
		appendText(text, Color.BLUE);
	}

}
